<?php
namespace app\common\tool;

class Tencent
{
    private $commomparams;

    private static $instance = null;
    
    protected $tencent_api_urls = [
        'ACCESS_TOKEN'       => 'https://api.e.qq.com/oauth/token',                     //auth_code获取access_token
        'GET_ADVERTISER'     => 'https://api.e.qq.com/v1.3/advertiser/get',             //获取广告主账号信息
        'GET_BALANCE'        => 'https://api.e.qq.com/v1.3/funds/get',                  //获取资金账户信息   
        'GET_PLANS'          => 'https://api.e.qq.com/v1.3/campaigns/get',              //获取账户计划信息
        'GET_CAMPAIGN'       => 'https://api.e.qq.com/v1.3/adgroups/get',               //获取广告组
        'EDIT_CAMPAIGN'      => 'https://api.e.qq.com/v1.3/adgroups/update',            //修改广告组
        'GET_ADVERTISEMENT'  => 'https://api.e.qq.com/v1.3/ads/get'                     //获取广告
    ];

    // 禁止被实例化
    // 初始公共Header和公共参数
    public function __construct()
    {
        $this->commomparams = [
            'app_key'       => Config('unidesk.appkey'),
            'timestamp'     => date('Y-m-d H:i:s'),
            'v'             => '2.0',
            'sign_method'   => 'md5',
            'format'        => 'json',
            'simplify'      => true  
        ];
    }
 
    // 禁止clone
    private function __clone()
    {
        
    }

    // 实例化自己并保存到$instance中，已实例化则直接调用
    public static function getInstance()
    {
        if (empty(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * 获取广告主账号
     * @param string access_token
     * @param string account_id
     */
    public function get_advertiser_accounts($access_token,$account_id)
    {
        $params = [
            'access_token'  => $access_token,
            'timestamp'     => time(),
            'agency_id'     => 9627777,
            'account_id'    => $account_id,
            'fields'        => ['account_id','corporation_name'],
            'page_size'     => 100,
            'nonce'         => md5(uniqid('', true)),
            'page'          => 1,
        ];
        $arr = [];

        foreach ($params as $key => $value) {
            if (!is_string($value)) {
                $params[$key] = json_encode($value);
            }
        }

        $request_url = $this->tencent_api_urls['GET_ADVERTISER'] . '?' . http_build_query($params);
        $res = httpRequest($request_url);
        
        if(isset($res['code']) && $res['code'] !== 0 ){
            return ['errmsg' => !empty($res['message_cn']) ? $res['message_cn'] : '请求获取广告主信息失败!' ];
        }
        if(!empty($res['data']['list'])){
            foreach ($res['data']['list'] as $key => $value) {
                //获取余额
                // $balance = $this->get_account_balance($access_token,$value['account_id']);
                $item = [
                    'account_id'            => $value['account_id'],
                    'account_name'          => $value['corporation_name'],
                ];
                $arr[] = $item;        
            }    
        }

        return $arr;
    }
}